package com.example.cgpsys.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.cgpsys.beans.Cart;
import com.example.cgpsys.beans.Order;
import com.example.cgpsys.beans.Result;
import com.example.cgpsys.beans.Product;
import com.example.cgpsys.dao.CartDao;
import com.example.cgpsys.dao.OrderDao;
import com.example.cgpsys.dao.ProductDao;
import com.example.cgpsys.other.config.SystemFileConfig;
import com.example.cgpsys.other.utils.FileUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

@RequestMapping("/product")
@RestController
public class ProductController {

    private final ProductDao productDao;

    private final OrderDao orderDao;

    private final CartDao cartDao;

    public ProductController(ProductDao productDao, OrderDao orderDao, CartDao cartDao) {
        this.productDao = productDao;
        this.orderDao = orderDao;
        this.cartDao = cartDao;
    }

    @GetMapping("/list")
    public Result loadProductList(String name, String begin, String end){
        QueryWrapper<Product> queryWrapper = new QueryWrapper<>();
        if (name!=null){
            queryWrapper.eq("name",name);
        }
        if (begin!=null){
            queryWrapper.ge("create_time",begin);
        }
        if (end!=null){
            queryWrapper.le("create_time",end);
        }
        List<Product> productList = productDao.selectList(queryWrapper);
        for(Product p:productList){
            QueryWrapper<Order> orderQueryWrapper = new QueryWrapper<>();
            orderQueryWrapper.eq("name",p.getName());
            Integer count = orderDao.selectCount(orderQueryWrapper);
            p.setPeoples(count);
        }
        return Result.success(productList);
    }

    @PostMapping("/addOrUpdate")
    public Result addOrUpdateProduct(@RequestBody Product product){
        if (product.getId()==null){
            productDao.insert(product);
        }else{
            productDao.updateById(product);
        }
        return Result.success();
    }

    @PostMapping("/upload")
    public Result uploadPicture(@RequestParam("picture") MultipartFile avatarFile, @RequestParam("id") Integer id) throws IOException {
        String uploadPath = FileUtils.uploadImage(SystemFileConfig.getFileUploadPath(), avatarFile);
        if ("#".equals(uploadPath)){
            return Result.error("图片类型错误");
        }
        Product product = new Product();
        product.setId(id);
        product.setPicture(uploadPath);
        productDao.updateById(product);
        return Result.success().put("imgUrl",uploadPath);
    }

    @PostMapping("/deleteProduct")
    public Result deleteProduct(@RequestBody List<Integer> idList){
        List<Product> productList = productDao.selectBatchIds(idList);
        productDao.deleteBatchIds(idList);
        for (Product product:productList){
            QueryWrapper<Cart> cartQueryWrapper = new QueryWrapper<>();
            cartQueryWrapper.eq("name",product.getName());
            cartDao.delete(cartQueryWrapper);
        }
        return Result.success();
    }

    @GetMapping("/getDetail")
    public Result loadDetail(Integer id){
        Product product = productDao.selectById(id);
        QueryWrapper<Order> orderQueryWrapper = new QueryWrapper<>();
        orderQueryWrapper.eq("name",product.getName());
        Integer count = orderDao.selectCount(orderQueryWrapper);
        product.setPeoples(count);
        return Result.success(product);
    }

}
